package com.bwf.tcp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * @Version v1.0
 * @Description: TODO
 * @Author ZHANZHONGXIN
 * @Date 2022/5/26 13:35
 * @Copyright 博为峰
 */
public class ClientDemo {
    public static void main(String[] args) throws IOException {
        /*
           TCP:客户端
           1.创建tcp客户端socket服务，使用Socket类，指定ip和端口
           2.获取字节流对象，将数据写出
           3.关闭资源
         */
        Socket socket = new Socket("172.20.255.74", 9999);
        //向服务器端写数据
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("张三".getBytes());
        //从服务器端读数据
        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int i = inputStream.read(bytes);
        String str = new String(bytes, 0, i);
        System.out.println("读到的数据 = " + str);
        socket.close();
    }
}
